package com.eascs.ticket.project.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.app.exception.ServiceException;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.project.dao.ProjectModuleDao;
import com.eascs.ticket.project.entity.ProjectModule;
import com.eascs.ticket.project.entity.ProjectModuleUpdate;

@Service
public class ProjectModuleService extends BaseSevice {

	@Inject
	private ProjectModuleDao projectModuleDao;

	public List<Object[]> getProjectModuleDataSouceByProjectId(String projectId) {
		return this.projectModuleDao.getProjectModuleDataSouceByProjectId(projectId);
	}

	public List<Object[]> getProjectModuleByProjectId(String projectId) {
		return this.projectModuleDao.getProjectModuleByProjectId(projectId);
	}
	
	public List<Object[]> getProjectModuleByNameCodeProjectId(String projectId) {
		return this.projectModuleDao.getProjectModuleCodeNameByProjectId(projectId);
	}

	public ProjectModule getProjectModuleById(String projectModuleId) {
		return this.proxyDao.getById(ProjectModule.class, projectModuleId);
	}

	public List<Object[]> getAllProjectModuleDataSouce() {
		return this.proxyDao.findObjectsList("sql.ProjectModuleService.getAllProjectModuleDataSouce");
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void insertProjectModule(ProjectModule projectModule) {
		EntityUtil.initInsertEntity(projectModule);
		this.proxyDao.insert(projectModule);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void updateProjectModule(ProjectModuleUpdate projectModuleUpdate) {
		EntityUtil.initUpdateEntity(projectModuleUpdate);
		this.proxyDao.updateById(projectModuleUpdate);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void removeProjectModule(String id) {
		ProjectModule projectModule = this.proxyDao.getById(ProjectModule.class, id);
		if (this.projectModuleDao.countProjectModuleInTicket(projectModule.getProjectId(), id) > 0) {
			throw new ServiceException("该项目模块已经被使用，无法删除！");
		}
		this.proxyDao.deleteByIdWithEntity(projectModule);
	}

}
